package cn.udrm.water.base.v1.manager.entities;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.NamedEntityGraphs;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.hibernate.annotations.GenericGenerator;

/**
 * 系统资源
 * @author liukang
 * @date 2019年3月22日
 */
@Getter
@Setter
@NoArgsConstructor
@Entity(name="BaseResource")
@Table(name="BASE_AC_RESOURCE")
@NamedEntityGraphs({
	@NamedEntityGraph(name="BaseResource.roles",
		attributeNodes={
		@NamedAttributeNode("roles")
	}),
	@NamedEntityGraph(name="BaseResource.rolesAndParent",
		attributeNodes={
		@NamedAttributeNode("roles"),
		@NamedAttributeNode("parent")
	}),
	@NamedEntityGraph(name="BaseResource.childrens",
		attributeNodes={
		@NamedAttributeNode("childrens")
	}),
	@NamedEntityGraph(name="BaseResource.parent",
		attributeNodes={
		@NamedAttributeNode("parent")
	})
})
public class BaseResource implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GenericGenerator(name="idGenerator", strategy="uuid")
	@GeneratedValue(generator="idGenerator")
	@Column(name="ID")
	private String id;
	
	/*
	 * 资源名称
	 */
	private String name;
	private String enName;
	
	/*
	 * 资源自关联
	 */
	@ManyToOne(optional=true)
	@JoinColumn(name="PARENT_ID")
	private BaseResource parent;
	@OneToMany(mappedBy="parent")
	private List<BaseResource> childrens;
	
	/*
	 * 资源所属角色
	 */
	@ManyToMany
    @JoinTable(name="BASE_AC_RESOURCE_ROLE", joinColumns=@JoinColumn(name="RESOURCE_GUID"),
    			inverseJoinColumns=@JoinColumn(name="ROLE_GUID"))
	private List<BaseRole> roles;

	public BaseResource(String id) {
		super();
		this.id = id;
	}
}
